<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Ham load nhanh view tuong duong $this->load->view()
 * @param string $template
 * @param array $data
 */
function view_load($template, $data = null) {
	if (!file_exists(APPPATH . 'views/' . $template . '.php'))
		return;
	$CI = & get_instance();
	if (is_array($data))
		$CI->load->view($template, $data);
	$CI->load->view($template);
}

/**
 * Đường dẫn thư mục theme
 * @param type $path
 * @return type
 */
function style_path($path = '') {
	$path = preg_replace('/(.+)(\/)?/i', '$1/', $path);
	return base_url() . 'styles/' . $path;
}

/**
 * Lấy avatar của fb
 * @param type $fb_id
 * @return type
 */
function fb_avatar($fb_id) {
	return 'https://graph.facebook.com/' . $fb_id . '/picture';
}

/**
 * 
 * @param type $time
 * @return string
 */
function time2str($time) {
	$CI = &get_instance();
	$CI->load->helper('date');
//	return timespan(0,now()-$time) . lang ('ago');
	$words = array(
		'second' => 60,
		'minute' => 60,
		'hour' => 24,
		'day' => 7,
		'week' => 29,
	);
	$sub = now() - $time;
	if (!ini_get('date.timezone')) {
		date_default_timezone_set('GMT');
	}
	$result = lang('on_date') . mdate($CI->config->item('CI_date_format'), $time);
	foreach ($words as $word => $key) {
//		echo $key . ' - ';
		if ($sub % $key == 1)
			$result = $sub % $key . lang($word) . lang('ago');
		else if ($sub % $key > 1)
			$result = $sub % $key . lang($word . 's') . lang('ago');
		$sub = (int) ($sub / $key);
	}
	return $result;
}

function striplongstr($str, $max_str = 18) {
//	$max_str=18;
	return strlen($str) > $max_str ? substr($str, 0, $max_str - 3) . '...' : $str;
}

function smileys_parse($str) {
	$CI = &get_instance();
//	echo base_url() . $CI->config->item('smiley_path');die;
	return parse_smileys($str, base_url() . $CI->config->item('smiley_path'));
}

function youtube_embed($video_id)
{
	return 'http://www.youtube.com/v/' . $video_id . '?version=3&amp;autohide=1';
}
?>
